//
//  UIButton+Extensions.swift
//  BasicApp
//
//  Created by Wangtao0714 on 2021/7/7.
//  Copyright © 2021 Facebook. All rights reserved.
//

import UIKit

var expandSizeKey = "expandSizeKey"

extension UIButton {
    open func ssc_expandSize(size:CGFloat) {
            objc_setAssociatedObject(self, &expandSizeKey,size, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        
        private func expandRect() -> CGRect {
            let expandSize = objc_getAssociatedObject(self, &expandSizeKey)
            if (expandSize != nil) {
                return CGRect(x: bounds.origin.x - (expandSize as! CGFloat), y: bounds.origin.y - (expandSize as! CGFloat), width: bounds.size.width + 2*(expandSize as! CGFloat), height: bounds.size.height + 2*(expandSize as! CGFloat))
            }else{
                return bounds;
            }
        }
        
        open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            let buttonRect =  expandRect()
            if (buttonRect.equalTo(bounds)) {
                return super.point(inside: point, with: event)
            }else{
                return buttonRect.contains(point)
            }
        }
   
}


extension UIButton {
    /// 快速创建按钮(全)
    ///
    /// - Parameters:
    ///   - imageName: 图片
    ///   - titleColor: 字体颜色
    ///   - titleFont: 字体大小
    ///   - backgroundColor: 背景颜色
    ///   - title: 标题
    /// - Returns: button
    static func buttonWith(imageName: String?, titleColor: UIColor?, titleFont: UIFont?, backgroundColor: UIColor?, title: String?) -> UIButton {
        let button = UIButton(type: .custom)
        button.backgroundColor = backgroundColor
        if title?.count != nil {
            button.setTitle(title, for: .normal)
            button.setTitleColor(titleColor, for: .normal)
        }
        if titleFont != nil {
            button.titleLabel?.font = titleFont
        }
        if (imageName?.count) != nil {
            button.setImage(UIImage(named: imageName!), for: .normal)
            if title?.count != nil {
                button.titleEdgeInsets = UIEdgeInsets(top: 0, left: UIScreen.main.bounds.size.width / 375, bottom: 0, right: 0)
                button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: UIScreen.main.bounds.size.width / 375)
            }
        }
        return button
    }
    /// 快速创建按钮(背景图片+文字)
    ///
    /// - Parameters:
    ///   - bgImageName: 图片
    ///   - titleColor: 字体颜色
    ///   - titleFont: 字体大小
    ///   - backgroundColor: 背景颜色
    ///   - title: 标题
    /// - Returns: button
    static func buttonWith(bgImageName:String?, titleColor: UIColor?, titleFont: UIFont?, title: String?) -> UIButton {
        let button = UIButton(type: .custom)
        if title?.count != nil {
            button.setTitle(title, for: .normal)
            button.setTitleColor(titleColor, for: .normal)
        }
        if titleFont != nil {
            button.titleLabel?.font = titleFont
        }
        if (bgImageName?.count) != nil {
            button.setBackgroundImage(UIImage(named: bgImageName!), for: .normal)
        }
        return button
    }
    /// 快速创建按钮(无图片)
    ///
    /// - Parameters:
    ///   - titleColor: 字体颜色
    ///   - titleFont: 字体大小
    ///   - backgroundColor: 背景颜色
    ///   - title: 标题
    /// - Returns: button
    static func buttonWith(titleColor: UIColor?, titleFont: UIFont?, backgroundColor: UIColor?, title: String?) -> UIButton {
        return UIButton.buttonWith(imageName: nil, titleColor: titleColor, titleFont: titleFont, backgroundColor: backgroundColor, title: title)
    }
    /// 快速创建按钮（图片）
    ///
    /// - Parameters:
    ///   - imageName: 图片
    ///   - backgroundColor: 背景颜色
    /// - Returns: button
    static func buttonWith(imageName: String?,backgroundColor: UIColor?) -> UIButton {
        return UIButton.buttonWith(imageName: imageName, titleColor: nil, titleFont: nil, backgroundColor: backgroundColor, title: nil)
    }
    /// 快速创建按钮(背景颜色)
    ///
    /// - Parameters:
    ///   - backgroundColor: 背景颜色
    /// - Returns: button
    static func buttonWith(backgroundColor: UIColor?) -> UIButton {
        return UIButton.buttonWith(imageName: nil, titleColor: nil, titleFont: nil, backgroundColor: backgroundColor, title: nil)
    }
}
extension UIButton {
    
    /// 倒计时
    ///
    /// - Parameters:
    ///   - timeLine: 倒计时总时间
    ///   - title: 还没倒计时的title
    ///   - mainColor: 还没倒计时的颜色
    ///   - countColor: 倒计时的颜色
    func startWith(timeLine: TimeInterval, title: String?, mainColor:UIColor?, countColor: UIColor?) {
        var time: Int = Int(timeLine)
        let codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
        codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000))  //此处方法与Swift 3.0 不同
        codeTimer.setEventHandler(handler: {
            time -= 1
            DispatchQueue.main.async {
                self.isUserInteractionEnabled = false
            }
            if time < 0 {
                codeTimer.cancel()
                DispatchQueue.main.async {
                    self.isUserInteractionEnabled = true
                    self.setTitle(title, for: .normal)
                    self.setTitleColor(mainColor, for: .normal)
                    self.layer.borderColor = mainColor?.cgColor
                }
                return
            }
            DispatchQueue.main.async {
                self.setTitle("\(time)", for: .normal)
                self.setTitleColor(countColor, for: .normal)
                self.layer.borderColor = countColor?.cgColor
            }
        })
        codeTimer.activate()
    }
}
